
package dbconnection;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//import java.util.Locale;


public class DBConnection {
    
//    static {
//        Locale.setDefault(Locale.US);
//    }
    
    private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private final String DB_CONNECTION;
    private final String DB_USER;
    private final String DB_PASSWORD;
    
    private Connection connection = null;
    
    public DBConnection(String DB_CONNECTION, String DB_USER, String DB_PASSWORD) {
        
        this.DB_CONNECTION = DB_CONNECTION;
        this.DB_USER = DB_USER;
        this.DB_PASSWORD = DB_PASSWORD;
        
        try {
            connect();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        
    }
    private void connect() throws SQLException {

        try {
            Class.forName(DB_DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        
        connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
        
    }
    
    public void printTable(String table) {
     
        if (table == null) return;
        
        String selectStatement = "select * from " + table;
        try {
            Statement statement = connection.createStatement();
            ResultSet results = statement.executeQuery(selectStatement);
            String cout = "";
            for (int i = 1; i < results.getMetaData().getColumnCount(); i++) 
                cout += String.format("%10s", results.getMetaData().getColumnLabel(i));
            System.out.println(cout);
            while (results.next()) {
                cout = "";
                for (int i = 1; i < results.getMetaData().getColumnCount(); i++)
                    cout += String.format("%10s", results.getString(i));
                System.out.println(cout);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        
    }
    
}
